<?php

namespace app\admin\controller\dealer;

use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="订购订单")
 */
class OrderIndent extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\mall\Order();
        
    }

    /**
     * @NodeAnotation(title="订购商品订单列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->where('pay_status',1)
                ->where('indent_status',1)
                ->count();
            $list = $this->model
                ->where($where)
                ->where('pay_status',1)
                ->where('indent_status',1)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            $goodsModel = new \app\admin\model\mall\Goods;
            $goodsData = $goodsModel->find(2);
            foreach ($list as $key => &$value) {
                $value['goods_name'] = $goodsData->name;
                $value['goods_images'] = $goodsData->images;
            }

            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        $this->layoutBgColor();
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="订购转普通订单")
     */
    public function indentUpdate()
    {
        $post = $this->request->post();
        // print_r($post);die;
        $rule = [
            'id|订单标识'    => 'require',
        ];
        $this->validate($post, $rule);
        $row = $this->model->find($post['id']);
        if (!$row) {
            $this->error('数据不存在');
        }
        try {
            $row->indent_status = 0;
            $row->update_time = time();
            $row->save();
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('保存成功');
    }
}